
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>

#define PORT 12345
int main()
{
    int cntl_flag = 0, login_flag = 0;
    int sockfd = -1, connectfd = -1;
    char buf[20] = " ", name[20] = " ", password[20] = " ", result[20] = " ";
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd)
    {
        perror("socket");
        return -1;
    }
LINK:
    connectfd = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    if (-1 == connectfd)
    {
        perror("connect");
        return -1;
    }
    while (1)
    {
        if (connectfd == -1)
        {
            printf("等待连接服务器...");
            goto LINK;
        }
        if (login_flag == 0)
        {
            printf("1.用户登录 2.用户注册 0.关闭系统\n请输入功能编号\n");
            scanf("%d", &cntl_flag);
            if (cntl_flag == 0) // 退出系统
            {
                shutdown(connectfd, SHUT_RDWR);
                printf("正在退出程序。。\n");
                return 0;
            }
            else if (cntl_flag == 1) // 登录操作
            {
                strcpy(buf, "login");
                write(sockfd, buf, sizeof(buf));
                printf("请输入登录账号：\n");
                scanf("%s", name);
                write(sockfd, name, sizeof(name));
                printf("请输入密码：\n");
                scanf("%s", password);
                write(sockfd, password, sizeof(password));
                printf("发送登录消息\n等待服务器响应。。。\n");
                read(sockfd, result, sizeof(result));
                if (strcmp(result, "200") == 0)
                {
                    printf("登陆成功！\n");
                    login_flag = 1;
                }
                else if (strcmp(result, "501") == 0) // 用户密码错误
                {
                    printf("登录失败！用户密码错误\n");
                }
                else if (strcmp(result, "502") == 0) // 用户不存在
                {
                    printf("登录失败！用户不存在\n");
                }
                else
                {
                    printf("服务器错误！请重试！\n");
                    // connectfd = -1;
                    // printf("尝试断开重连！\n");
                }
            }
            else if (cntl_flag == 2) // 注册操作
            {
                strcpy(buf, "register");
                write(sockfd, buf, sizeof(buf));
                printf("请输入注册账号：\n");
                scanf("%s", name);
                write(sockfd, name, sizeof(name));
                printf("请输入密码：\n");
                scanf("%s", password);
                write(sockfd, password, sizeof(password));
                printf("发送注册消息。。。\n等待服务器响应。。。\n");
                read(sockfd, result, sizeof(result));
                if (strcmp(result, "200") == 0)
                {
                    printf("注册成功！进入程序！\n");
                    login_flag = 1;
                }
                else if (strcmp(result, "501") == 0) // 用户已存在
                {
                    printf("注册失败！用户已存在\n");
                }
                else
                {
                    printf("服务器错误！请重试！\n");
                    // connectfd = -1;
                    // printf("尝试断开重连！\n");
                }
            }
            else
            {
                printf("输入不合法\n");
            }
        }
        else
        {
            printf("3.修改密码 4. 删除账户 5.退出登录 0.关闭系统\n请输入功能编号\n");
            scanf("%d", &cntl_flag);
            if (cntl_flag == 0) // 退出系统
            {
                strcpy(buf, "exit");
                write(sockfd, buf, sizeof(buf));
                printf("发送退出登录消息\n等待服务器响应。。。\n");
                read(sockfd, result, sizeof(result));
                if (strcmp(result, "200") == 0)
                {
                    printf("已删除用户信息！\n");
                    login_flag = 0;
                    shutdown(connectfd, SHUT_RDWR);
                    printf("已退出登录。。\n");
                    printf("正在退出系统。。\n");
                    return 0;
                }
                else
                {
                    printf("服务器错误！请重试！\n");
                    // connectfd = -1;
                    // printf("尝试断开重连！\n");
                }
            }
            // 修改密码操作
            else if (cntl_flag == 3)
            {
                char tem_password[20];
                strcpy(buf, "change");
                write(sockfd, buf, sizeof(buf));
                printf("请输入原密码：\n");
                write(sockfd, name, sizeof(name));
                scanf("%s", password);
                printf("发送验证密码消息\n");
                write(sockfd, password, sizeof(password));
                printf("等待服务器响应。。。\n");
                read(sockfd, result, sizeof(result));
                if (strcmp(result, "200") == 0)
                {
                INPUT_PWD:
                    printf("请输入新密码：\n");
                    scanf("%s", password);
                    printf("请再次确认新密码：\n");
                    scanf("%s", tem_password);
                    if (strcmp(password, tem_password) != 0)
                    {
                        printf("两次输入密码不一致！请重新输入\n");
                        goto INPUT_PWD;
                    }
                    else
                    {
                        write(sockfd, password, sizeof(password));
                        printf("发送修改密码消息\n等待服务器响应。。。\n");
                        read(sockfd, result, sizeof(result));
                        if (strcmp(result, "200") == 0)
                        {
                            printf("修改密码成功,请重新登录！\n");
                            login_flag = 0;
                        }
                        else
                        {
                            printf("服务器错误！请重试！\n");
                            // connectfd = -1;
                            // printf("尝试断开重连！\n");
                        }
                    }
                }
                else if (strcmp(result, "501") == 0) // 用户密码错误
                {
                    printf("修改失败！用户原密码错误\n");
                }
                else
                {
                    printf("服务器错误！请重试！\n");
                    // connectfd = -1;
                    // printf("尝试断开重连！\n");
                }
            }
            else if (cntl_flag == 4) // 删除账户
            {
                int pwd_err_count = 3;
                strcpy(buf, "remove");
                printf("删除账户会移除所有用户数据！！！\n请输入密码以确认删除!\n");
                scanf("%s", password);
            SURE_PWD:
                write(sockfd, buf, sizeof(buf));
                write(sockfd, name, sizeof(name));
                printf("发送验证密码消息\n");
                write(sockfd, password, sizeof(password));
                printf("等待服务器响应。。。\n");
                read(sockfd, result, sizeof(result));
                if (strcmp(result, "200") == 0)
                {
                    printf("已删除用户信息！\n");
                    login_flag = 0;

                    printf("已退出登录。。\n");
                }
                else if (strcmp(result, "501") == 0)
                {
                    if (pwd_err_count > 0)
                    {
                        printf("密码错误！%d次后会强制退出账户\n", pwd_err_count--);
                        printf("请输入密码以确认删除！\n");
                        scanf("%s", password);
                        goto SURE_PWD;
                    }
                    login_flag = 0;

                    printf("已强制退出登录。。\n");
                }
                else
                {
                    printf("服务器错误！请重试！\n");
                    // connectfd = -1;
                    // printf("尝试断开重连！\n");
                }
            }
            else if (cntl_flag == 5) // 退出登录
            {
                strcpy(buf, "exit");
                write(sockfd, buf, sizeof(buf));
                printf("发送退出登录消息\n等待服务器响应。。。\n");
                read(sockfd, result, sizeof(result));
                if (strcmp(result, "200") == 0)
                {
                    printf("已删除本地用户信息！\n");
                    login_flag = 0;

                    printf("已退出登录。。\n");
                }
                else
                {
                    printf("服务器错误！请重试！\n");
                    // connectfd = -1;
                    // printf("尝试断开重连！\n");
                }
            }
            else
            {
                printf("输入不合法\n");
            }
        }
    }
}
